<?php

class ProjectNationalityController extends RController
{
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/staff.php'.
     */
    public $layout='//layouts/staff';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'rights',
        );
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }

    /**
     * Creates a new model.
     * @param $projectId
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate($id)
    {
        $model=new ProjectNationality;
        $model->project_id = $id;

        $projectModel = $model->project;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['ProjectNationality']))
        {
            // check data in database
            $check = $model->getByProjectIdAndNationalityId(
                        $_POST['ProjectNationality']['project_id'], $_POST['ProjectNationality']['nationality_id']);
            if (isset($check)) {
                //if count > 0 update data to active = 1 and delmark = 0
                $model = $check;
                $model->active = 1;
                $model->delmark = 0;
            }

            $model->attributes=$_POST['ProjectNationality'];

            if($model->save())
                $this->redirect(array('project/update/'.$id.'/#projectCountry'));
        }

        $this->render('create',array(
            'model'=>$model,
            'projectModel'=>$projectModel,
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        $projectModel = $model->project;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['ProjectNationality']))
        {
            $model->attributes=$_POST['ProjectNationality'];
            if($model->save())
                $this->redirect(array('project/update/'.$id.'/#projectCountry'));
        }

        $this->render('update',array(
            'model'=>$model,
            'projectModel'=>$projectModel,
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            $model = $this->loadModel($id);
            $model->delmark = true;
            $model->active = 0;
            $model->save();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Lists all models.
     */
    public function actionList()
    {
        $criteria = new CDbCriteria();

        if(isset($_GET['q']))
        {
            $q = $_GET['q'];
            $criteria->compare('project_id', $q, true, 'OR');
            $criteria->compare('nationality_id', $q, true, 'OR');
        }

        $criteria->compare('delmark', '0', true, 'AND');

        $dataProvider=new CActiveDataProvider('ProjectNationality', array(
            'criteria'=> $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        $this->render('list',array(
            'dataProvider'=>$dataProvider,
        ));
    }

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model=new ProjectNationality('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['ProjectNationality']))
            $model->attributes=$_GET['ProjectNationality'];

        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model=ProjectNationality::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='project-nationality-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    /**
     *Return dropdown by project id
     */
    public function actionGetOptionByProjectId() {
        $modelsList = ProjectNationality::model()->getActiveByProjectId($_POST['projectId']);

        $modelsList=CHtml::listData($modelsList,'nationality.id','nationality.country');
        if(count($modelsList) > 0) {
            echo CHtml::tag('option',
                array('value'=>null),CHtml::encode('-- Please select --'),true);
        }
        foreach($modelsList as $value=>$name)
        {
            echo CHtml::tag('option',
                    array('value'=>$value),CHtml::encode($name),true);
        }
    }
}
